// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.linqingying.gitcode.api.data;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.intellij.openapi.util.text.StringUtil;
import com.linqingying.gitcode.api.GCRepositoryPath;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
@JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("UnusedDeclaration")
public class GitCodeRepoBasic {
    private Long id;
    //private String nodeId;
    private String name;
    private String fullName;
    private GitCodeUser owner;
    @JsonProperty("private")
    private Boolean isPrivate;
    @JsonProperty("web_url")
    private String webUrl;

    @JsonProperty("ssh_url_to_repo")
    private String sshUrlToRepo;
    @JsonProperty("http_url_to_repo")
    private String httpUrlToRepo;
    private String description;
    @JsonProperty("fork")
    private Boolean isFork;
    // display [owner]/[repo_name]
    @JsonProperty("human_name")
    private String humanName;
    private String url;

    public String getHttpUrlToRepo() {
        return httpUrlToRepo;
    }

    public String getSshUrlToRepo() {
        return sshUrlToRepo;
    }
    //urls

    @NotNull
    public String getName() {
        return name;
    }

    @NotNull
    public String getHumanUserName() {
        return humanName.split("/")[0];
    }

    @NotNull
    public String getHumanName() {
        if(humanName == null){
            return fullName;
        }
         return humanName;

    }

    @NotNull
    public String getDescription() {
        return StringUtil.notNullize(description);
    }

    public boolean isPrivate() {
        return isPrivate;
    }

    public boolean isFork() {
        return isFork;
    }

    @NotNull
    public String getUrl() {
        return url;
    }

    @NotNull
    public String getWebUrl() {
        return webUrl;
    }

    @Nullable
    public GitCodeUser getOwner() {
        return owner;
    }


    @Nullable
    public String getUserName() {
        if (getOwner() != null) {
            return getOwner().getLogin();
        }
        return null;
    }

    @NotNull
    public String getFullName() {
        return fullName;
    }

    @NotNull
    public GCRepositoryPath getFullPath() {
        String[] split = fullName.split("/");
        return new GCRepositoryPath(split[0], split[1]);
    }

    @Override
    public String toString() {
        return "GitCodeRepo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof GitCodeRepoBasic basic)) return false;
        return id.equals(basic.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
